package cn.wen.yaling.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.wen.yaling.aop.annotation.operation.OperationLogger;
import cn.wen.yaling.aop.annotation.repeat.AvoidRepeatableCommit;
import cn.wen.yaling.commons.vo.RoleAddVO;
import cn.wen.yaling.commons.vo.RoleMenuUpdateVO;
import cn.wen.yaling.commons.vo.RolePermissionUpdateVO;
import cn.wen.yaling.commons.vo.RoleUpdateVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.commons.entity.RoleEntity;
import cn.wen.yaling.xo.service.RoleService;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.ResultEntity;



/**
 * 
 * 角色控制层
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:36
 */
@RestController
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "获取角色信息列表", notes = "获取角色信息列表")
    @GetMapping("/admin/role/list")
    public ResultEntity getList() {
        return roleService.getRoleList();
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "新增角色信息")
    @ApiOperation(value = "新增角色信息", notes = "新增角色信息")
    @PostMapping("/admin/role/add")
    public ResultEntity add(@RequestBody RoleAddVO roleAddVO) {
        return roleService.addRole(roleAddVO);
    }

    @OperationLogger(value = "更新角色信息")
    @ApiOperation(value = "更新角色信息", notes = "更新角色信息")
    @PostMapping("/admin/role/update")
    public ResultEntity update(@RequestBody RoleUpdateVO roleVO) {
        return roleService.editRole(roleVO);
    }

    @OperationLogger(value = "删除角色信息")
    @ApiOperation(value = "删除角色信息", notes = "删除角色信息")
    @PostMapping("/admin/role/delete")
    public ResultEntity delete(@RequestParam("roleIds") List<Integer> roleIds) {
        return roleService.deleteBatchRole(roleIds);
    }

    @ApiOperation(value = "获取菜单权限", notes = "获取菜单权限")
    @GetMapping("/admin/role/menu/list")
    public ResultEntity updateRoleMenu(@RequestParam("roleId") Integer roleId) {
        return roleService.getRoleMenuListById(roleId);
    }

    @ApiOperation(value = "获取权限", notes = "获取权限")
    @GetMapping("/admin/role/permission/list")
    public ResultEntity updateRolePermission(@RequestParam("roleId") Integer roleId) {
        return roleService.getRolePermissionListById(roleId);
    }


    @OperationLogger(value = "分配菜单权限")
    @ApiOperation(value = "分配菜单权限", notes = "分配菜单权限")
    @PostMapping("/admin/role/menu/update")
    public ResultEntity updateRoleMenu(@RequestBody RoleMenuUpdateVO roleMenuVO) {
        return roleService.updateRoleMenu(roleMenuVO);
    }

    @OperationLogger(value = "分配数据权限")
    @ApiOperation(value = "分配数据权限", notes = "分配数据权限")
    @PostMapping("/admin/role/permission/update")
    public ResultEntity updateRolePermission(@RequestBody RolePermissionUpdateVO rolePermissionVO) {
        return roleService.updateRolePermission(rolePermissionVO);
    }

}
